package com.ch.web.admin.controller;

import com.ch.web.admin.entity.XyAdminEntity;
import com.ch.web.admin.request.AdminLoginRequestDto;
import com.ch.web.admin.service.XyAdminService;
import com.ch.web.annotation.IgnoreLogin;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author chenHao
 * @email 1429542285@qq.com
 * @date 2022-12-17 12:43:12
 */
@RestController
@RequestMapping("xy/admin")
public class XyAdminController {
    @Autowired
    private XyAdminService xyAdminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = xyAdminService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        XyAdminEntity xyAdmin = xyAdminService.getById(id);
        return R.ok().put("xyAdmin", xyAdmin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody XyAdminEntity xyAdmin) {
        xyAdminService.save(xyAdmin);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody XyAdminEntity xyAdmin) {
        xyAdminService.updateById(xyAdmin);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        xyAdminService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 管理员登录
     */
    @IgnoreLogin
    @RequestMapping("/login")
    public R adminLogin(@RequestBody AdminLoginRequestDto adminLoginRequestDto) throws Exception {
        return xyAdminService.adminLogin(adminLoginRequestDto);
    }

}
